home *** CD-ROM | disk | FTP | other *** search
- /*
-
- RemoveFile XCMD v1.2
-
- ©1990-1 Apple Computer, Inc.; by Mike Byrne
-
- RemoveFile takes a full pathname and deletes the file. It can also be "fooled" into deleting a folder
- if the pathname to the folder is passed without a colon at the end. The folder must be empty for this
- to work.
-
- Form:
- RemoveFile <full pathname>
-
- # the MPW 3.2 build commands:
- C -b RemoveFile.c -mbg off
- Link -w -t STAK -c WILD -rt XCMD=603 ∂
- -m ENTRYPOINT ∂
- -sg RemoveFile ∂
- RemoveFile.c.o ∂
- "{Libraries}HyperXLib.o" ∂
- "{Libraries}Runtime.o" ∂
- "{Libraries}Interface.o" ∂
- "{CLibraries}StdCLib.o" ∂
- -o "some stack"
- */
-
- #include <Types.h>
- #include <Packages.h>
- #include <string.h>
- #include <Memory.h>
- #include <Files.h>
- #include <Errors.h>
- #include "HyperXCmd.h"
-
- #define NULL 0L
- #define NIL OL
-
- #define kNumParams 1
-
-
- /* prototypes */
- void ErrorBack(XCmdPtr paramPtr, char *message);
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
- void UnlockParams ( XCmdPtr paramPtr, short paramCount );
-
-
- pascal void EntryPoint(XCmdPtr paramPtr)
- {
- short i,j;
- char volName[34];
- char ppathName[301];
- short vRefNum;
-
-
- /* move high and lock the parameters. */
- MoveLockParams(paramPtr, paramPtr->paramCount);
-
- /* check for copyright or syntax help request */
- if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
- ErrorBack(paramPtr, "v1.2, ©1990-1 Apple Computer, Inc.; by Mike Byrne");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
- ErrorBack(paramPtr, "RemoveFile syntax is 'RemoveFile(<full pathname>'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* not a copyright or help request. */
- /* check for correct number of parameters */
- if (paramPtr->paramCount != kNumParams) {
- ErrorBack(paramPtr, "Error: RemoveFile syntax is 'RemoveFile(<full pathname>'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
-
- /* extract the volume name from the handle, copy to a pas string,
- and get the volume reference number of the volume */
- for (i=0; ((*(paramPtr->params[0]))[i] != ':' && (i < 33)); i++)
- { volName[i] = (*(paramPtr->params[0]))[i]; }
- volName[i] = ':';
- volName[i+1] = '\0';
- c2pstr(volName);
- vRefNum = 0;
-
- if (SetVol(volName, vRefNum) != noErr) { // toolbox
- ErrorBack(paramPtr, "Error: Could not set the default volume");
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- if (GetVol(&volName, &vRefNum) != noErr) { // toolbox
- ErrorBack(paramPtr, "Error: Could not find the volume requested.");
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- /* now, copy the rest of the pathname to the partial pathname and convert it. */
- for (j=i; (j <= strlen((*(paramPtr->params[0]))) && (j < 300)); j++)
- { ppathName[j-i] = (*(paramPtr->params[0]))[j]; }
- c2pstr(ppathName);
-
- /* delete the file */
- switch (FSDelete(ppathName, vRefNum)) { // toolbox
- case noErr:
- return;
- break;
- case fBsyErr:
- ErrorBack(paramPtr, "Error: Cannot delete file: the file is busy.");
- UnlockParams(paramPtr, kNumParams);
- return;
- break;
- case fLckdErr, vLckdErr, wPrErr:
- ErrorBack(paramPtr, "Error: Cannot delete file: the file or volume is locked.");
- UnlockParams(paramPtr, kNumParams);
- return;
- break;
- case bdNamErr, fnfErr:
- ErrorBack(paramPtr, "Error: Cannot delete file: bad or non-existant file name.");
- UnlockParams(paramPtr, kNumParams);
- return;
- break;
- }
-
- }
-
-
-
-
-
-
- /* allocate and load up paramPtr->returnValue with a string
- -------------------------------------------------------- */
- void ErrorBack(XCmdPtr paramPtr, char *message)
- {
- Handle mesHnd;
-
- /*
- Allocate space for an error message.
- Copy the string into it.
- Return the handle to HyperCard.
- */
- mesHnd = NewHandle((long)(strlen(message)+1));
- if (mesHnd == nil) return;
- strcpy((char *)*mesHnd,message);
- paramPtr->returnValue = mesHnd;
- }
-
-
-
- /* move high and lock down all parameters (watch that these are read-only)
- ----------------------------------------------------------------------- */
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
- {
- short i;
-
- for(i=0; i <= paramCount-1; i++)
- {
- MoveHHi(paramPtr->params[i]);
- HLock(paramPtr->params[i]);
- }
- }
-
-
-
-
- /* unlock all parameter handles in the XCmdBlock
- --------------------------------------------- */
- void UnlockParams ( XCmdPtr paramPtr, short paramCount )
- { short i;
-
- for(i=0; i <= paramCount-1; i++)
- { HUnlock(paramPtr->params[i]);}
- }
-